package y24.m4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

public class CalendarApp extends JFrame {
    private JLabel monthLabel;
    private JPanel calendarPanel, controlPanel, infoPanel;
    private int currentMonth, currentYear;

    public CalendarApp() {
        setTitle("Calendar App");
        setLayout(new BorderLayout());

        currentMonth = 1; // 初始月份为1月
        currentYear = 2022; // 初始年份为2022
        monthLabel = new JLabel();

        JLabel monthLabel = new JLabel("Current Month: " + currentMonth + "/" + currentYear);
        monthLabel.setHorizontalAlignment(JLabel.CENTER);
        add(monthLabel, BorderLayout.NORTH);

        calendarPanel = new JPanel(new GridLayout(6, 7));
        updateCalendar(currentMonth, currentYear);

        controlPanel = new JPanel(new FlowLayout());
        JButton previousMonthButton = new JButton("Previous Month");
        JButton nextMonthButton = new JButton("Next Month");
        previousMonthButton.addActionListener(new PreviousMonthListener());
        nextMonthButton.addActionListener(new NextMonthListener());
        controlPanel.add(previousMonthButton);
        controlPanel.add(nextMonthButton);

        infoPanel = new JPanel(new FlowLayout());
        JLabel infoLabel = new JLabel("Some Information Here");
        infoPanel.add(infoLabel);

        add(calendarPanel, BorderLayout.CENTER);
        add(controlPanel, BorderLayout.SOUTH);
        add(infoPanel, BorderLayout.SOUTH);

        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void updateCalendar(int month, int year) {
        calendarPanel.removeAll();

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.YEAR, year);

        for (int i = 1; i <= calendar.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            JLabel label = new JLabel(String.valueOf(i));
            label.setHorizontalAlignment(JLabel.CENTER);
            calendarPanel.add(label);
        }

        monthLabel.setText("Current Month: " + month + "/" + year);
        revalidate();
    }

    private class PreviousMonthListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (currentMonth == 1) {
                currentMonth = 12;
                currentYear--;
            } else {
                currentMonth--;
            }

            updateCalendar(currentMonth, currentYear);
        }
    }

    private class NextMonthListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (currentMonth == 12) {
                currentMonth = 1;
                currentYear++;
            } else {
                currentMonth++;
            }

            updateCalendar(currentMonth, currentYear);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(CalendarApp::new);
    }
}
